package com.psl.services.work;

import com.psl.Dao.Conn;
import com.psl.model.halfproduct_kc;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class halfproductkc
{
  public List queryAllkc()
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    List zjkc = new ArrayList();
    try
    {
      pstmt = conn.prepareStatement("SELECT kc.h_no,li.h_name,kc.h_size,kc.h_r,kc.h_n,kc.h_num_old,kc.h_num_now from halfproduct_kc kc,halfproduct_list li WHERE kc.h_no=li.h_no");
      rs = pstmt.executeQuery();
      while (rs.next())
      {
        halfproduct_kc kc = new halfproduct_kc();
        kc.setH_no(rs.getString(1));
        kc.setH_name(rs.getString(2));
        kc.setH_size(rs.getString(3));
        kc.setH_r(rs.getString(4));
        kc.setH_n(rs.getString(5));
        kc.setH_num_old(rs.getInt(6));
        kc.setH_num_now(rs.getInt(7));
        zjkc.add(kc);
      }
      return zjkc;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
  }
  
  public boolean updateleftkc(int h_num_now, String h_no, String h_size, String h_r, String h_n)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("UPDATE halfproduct_kc set h_num_now=? where h_no=? and h_size=? and h_r=? and h_n=?");
      pstmt.setInt(1, h_num_now);
      pstmt.setString(2, h_no);
      pstmt.setString(3, h_size);
      pstmt.setString(4, h_r);
      pstmt.setString(5, h_n);
      pstmt.executeUpdate();
      HttpServletRequest request = ServletActionContext.getRequest();
      request.setAttribute("s", "<script>alert('出库成功！')</script>");
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
  
  public boolean updateoldkc(int h_num_old, String h_no, String h_size, String h_r, String h_n)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("UPDATE halfproduct_kc set h_num_old=? where h_no=? and h_size=? and h_r=? and h_n=?");
      pstmt.setInt(1, h_num_old);
      pstmt.setString(2, h_no);
      pstmt.setString(3, h_size);
      pstmt.setString(4, h_r);
      pstmt.setString(5, h_n);
      pstmt.executeUpdate();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
  
  public boolean updateoldkcto0()
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("UPDATE halfproduct_kc set h_num_old=0");
      pstmt.executeUpdate();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
  
  public boolean insertkc(String h_no, String h_size, String h_r, String h_n, int h_num_old)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("INSERT INTO halfproduct_kc(h_no,h_size,h_r,h_n,h_num_old) VALUES (?,?,?,?,?)");
      pstmt.setString(1, h_no);
      pstmt.setString(2, h_size);
      pstmt.setString(3, h_r);
      pstmt.setString(4, h_n);
      pstmt.setInt(5, h_num_old);
      pstmt.execute();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
}
